using System;
using bitzhuwei.Compiler;

namespace bitzhuwei.AnsiCFormat {
    /// <summary>
    /// Correspond to the Vn node translation_unit in the grammar(AnsiC).
    /// </summary>
    internal partial class Vntranslation_unit : IFullFormat {
        // [203] translation_unit = external_declaration ;
        // [204] translation_unit = translation_unit external_declaration ;



        private readonly TokenRange _scope;
        public TokenRange Scope => _scope;
        public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
            var config = new BlankConfig();
            context.PrintBlanksAnd(this.r0, preConfig, writer);
            var postfixCount = this.postfix.Count;
            for (var i = 0; i < postfixCount; i++) {
                if (i == 0) {
                    context.PrintCommentsBetween(this.postfix[i], this.r0, config, writer);
                }
                else {
                    context.PrintCommentsBetween(this.postfix[i], this.postfix[i - 1], config, writer);
                }
                context.PrintBlanksAnd(this.postfix[i], config, writer);
            }
        }

        private readonly List<Vnexternal_declaration> postfix = new();
        public void Add(Vnexternal_declaration r0) {
            this.postfix.Add(r0);
            this._scope.end = r0.Scope.end;
        }

        public readonly Vnexternal_declaration r0;
        public Vntranslation_unit(Vnexternal_declaration r0) {
            this.r0 = r0;
            this._scope = new TokenRange(r0);
        }


    }
}
